<?php
namespace Admin\Controller;

use Common\Model\CompanyListModel;
use Common\Model\TreeModel;

class CompanyListController extends AdminController{

	public function index(){
		$this->meta_title = '企业管理';
		$company_m = new CompanyListModel();
		$where_map = [];
		if(isset($_GET['name']) && !empty($_GET['name'])){
			$where_map['company_name'] = ['like', "%$_GET[name]%"];
		}
		$page = I('page', 1, 'intval'); // 默认显示第一页数据
		$row = 20;
		$data = $company_m
			->alias('g')
			->where($where_map)
			->order("g.is_order Desc,g.id desc")
			->page($page, $row)
			->select();
		foreach($data as $key => $val){
			$data[$key]['add_time'] = date("Y-m-d H:i:s", $val['add_time']);
		}

		$count = $company_m
			->alias('g')
			->where($where_map)
			->count();
		$list_data ['list_data'] = $data;
		// 分页
		if($count > $row){
			$page = new \Think\Page ($count, $row);
			$page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
			$list_data ['_page'] = $page->show();
		}
		$this->assign('all_count', $count);
		$this->assign('list_data', $list_data);
		$this->assign('getting', $_GET);
		$this->display();
	}

	public function edit(){
		$company_m = new CompanyListModel();
		if($_POST){
			$id = I('post.id', 0);
			$s_data['company_name'] = $_POST['company_name'] ? trim($_POST['company_name']) : '';
			$s_data['company_name_eg'] = $_POST['company_name_eg'] ? trim($_POST['company_name_eg']) : '';
			$s_data['company_logo'] = $_POST['company_logo'] ? $_POST['company_logo'] : '';
			$s_data['company_link_url'] = $_POST['company_link_url'] ? $_POST['company_link_url'] : '';
			$s_data['company_contact_weixin_img'] = $_POST['company_contact_weixin_img'] ? $_POST['company_contact_weixin_img'] : '';
			$s_data['company_jianjie'] = $_POST['company_jianjie'] ? $_POST['company_jianjie'] : '';
			$s_data['yewu_jianjie'] = $_POST['yewu_jianjie'] ? $_POST['yewu_jianjie'] : '';
			$s_data['c_www_url'] = $_POST['c_www_url'] ? $_POST['c_www_url'] : '';
			$s_data['c_fax'] = $_POST['c_fax'] ? $_POST['c_fax'] : '';
			$s_data['c_telephone'] = $_POST['c_telephone'] ? $_POST['c_telephone'] : '';
			$s_data['company_address'] = $_POST['company_address'] ? $_POST['company_address'] : '';
			$s_data['company_address_eg'] = $_POST['company_address_eg'] ? $_POST['company_address_eg'] : '';
			$s_data['zhengquan_name'] = $_POST['zhengquan_name'] ? $_POST['zhengquan_name'] : '';
			$s_data['zhengquan_num'] = $_POST['zhengquan_num'] ? $_POST['zhengquan_num'] : '';
			$s_data['company_lat'] = $_POST['company_lat'] ? $_POST['company_lat'] : 0;
			$s_data['company_lng'] = $_POST['company_lng'] ? $_POST['company_lng'] : 0;
			$s_data['company_jianjie_name'] = $_POST['company_jianjie_name'] ? $_POST['company_jianjie_name'] : '';
			$s_data['is_order'] = I('post.is_order', 99);
			$s_data['is_check'] = $_POST['is_check'] ? 1 : 0;
			$s_data['update_time'] = time();
			if(empty($s_data['company_name'])) $this->return_json_data(0, '请输入企业名称');
			//			$t_show_new = [];
			//			$show_arr = $_POST['show_arr']['nickname'];
			//			for($x = 0; $x < count($show_arr); $x++){
			//				$t_show_new[$x]['nickname'] = $_POST['show_arr']['nickname'][$x];
			//				$t_show_new[$x]['avatar'] = $_POST['show_arr']['avatar'][$x];
			//				$t_show_new[$x]['xiangqing'] = $_POST['show_arr']['xiangqing'][$x];
			//				if((empty($t_show_new[$x]['nickname']) || empty($t_show_new[$x]['avatar']))){
			//					$this->return_json_data(0, '请完整填写展示信息！');
			//				}
			//			}
			//			$s_data['show_arr'] = $t_show_new ? json_encode($t_show_new) : '';
			if($id > 0){
				$smap['id'] = $id;
				if(!$company_m->where($smap)->save($s_data)){
					$this->return_json_data(0, '对不起，企业信息更新失败，请重试');
				}
			}else{
				$s_data['add_time'] = time();
				if(!$insert_id = $company_m->add($s_data)){
					$this->return_json_data(0, '对不起，企业信息添加失败，请重试');
				}
			}
			$this->return_json_data(1, '企业信息操作成功');
		}
		$id = I('get.id', 0);
		if($id > 0){
			if(!$shop_info = $company_m->getInfo($id)){
				$this->error('对不起，企业信息不存在');
			}
			$shop_info['show_arr'] = $shop_info['show_arr'] ? json_decode($shop_info['show_arr'], true) : [];
		}else{
			$show_list_new[0]['nickname'] = '';
			$show_list_new[0]['avatar'] = '';
			$show_list_new[0]['xiangqing'] = '';
			$shop_info['show_arr'] = $show_list_new;
			$shop_info['id'] = 0;
			$shop_info['is_check'] = 1;
			$shop_info['is_order'] = 99;
		}
		$this->assign('info', $shop_info);
		$this->meta_title = '企业详情';
		$this->display('edit');
	}

	public function del(){
		$id = intval($_GET['id']);
		$company_m = new CompanyListModel();
		$map['id'] = $id;
		if(!$info = $company_m->getInfo($id)){
			$this->error('企业信息不存在,删除失败');
		}

		if($company_m->where($map)->delete()){
			$this->success('删除成功');
		}else{
			$this->error('删除失败，请重试');
		}
	}

	/**
	 * 统一ajax数据返回
	 *
	 * @param $code
	 * @param $info
	 */
	public function return_app_data($code, $info){
		echo json_encode(['code' => $code, 'info' => $info]);
		exit();
	}
}
